﻿using System;
using Microsoft.Xna.Framework;
using RainEngine.Rendering;
using RainEngine.SceneManagement;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using RainEngine.Effects;

namespace RainEngine.SceneNodes
{
    /// <summary>
    /// 创建一个球体
    /// </summary>
    public class SphereSceneNode:GenericSceneNode 
    {
        /// <summary>
        /// 球体水平方向切片数量
        /// </summary>
        private int  sphereCylinders=16;

        /// <summary>
        /// 球体竖直方向切片数量
        /// </summary>
        private int  sphereSlices;   
     
        /// <summary>
        /// 球体半径
        /// </summary>
        private float  radius;       
        
        /// <summary>
        /// 创建一个默认的球体，纬度有16段，经度有64段，半径为1。
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setDiffuseTextureName">纹理名称</param>
        /// <param name="radius">半径</param>
        public SphereSceneNode(GraphicsDevice graphicsDevice, ContentManager setContent, Scene setScene, string setDiffuseTextureName) : this(graphicsDevice, setContent, setScene,setDiffuseTextureName, null, 1, 16, 64) { }
        
        /// <summary>
        /// 创建一个SphereSceneNode对象。
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setDiffuseTextureName">漫反射纹理名称</param>
        /// /// <param name="setDiffuseTiles">漫反射纹理UV重复次数</param>
        /// <param name="setDetailTextureName">细节纹理名称</param>
        /// <param name="setDetailTiles">细节纹理UV重复次数</param>  
        /// <param name="radius">半径</param>
        /// <param name="cylinders">水平切片数量</param>
        /// <param name="slices">竖直切片数量</param>
        public SphereSceneNode(GraphicsDevice graphicsDevice, ContentManager setContent, Scene setScene, string setDiffuseTextureName, string setDetailTextureName, float radius, int cylinders, int slices)
            : base(graphicsDevice, setContent, setScene, setDiffuseTextureName, setDetailTextureName)
        {
            if (cylinders < 3 || slices < 3)
                throw new ArgumentException("slices/cylinders小于3无法计算SphereSceneNode");

            this.sphereCylinders = cylinders;
            this.sphereSlices = slices;
            this.radius = radius;
            this.Pose.LocalSize = this.radius;
            Vector3 scale = Vector3.One;
            this.Pose.SetScale(ref scale);
        }      
                
        /// <summary>
        /// 初始化节点。这个函数由引擎调用，不要直接使用它。
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            this.mesh =MeshBuilder .CreateSphere (graphicsDevice ,(byte) sphereCylinders, (byte)sphereSlices, this.radius);
        }

        #region 编辑器中可以编辑的属性
        
        /// <summary>
        /// 获取或设置球的半径。
        /// </summary>
        private float Radius
        {
            get 
            {
                return this.radius; 
            }
            set
            {
                this.radius = value;
                Pose.LocalSize = this.radius * 2; 
            }
        }
        
        /// <summary>
        /// 获取或设置This property is to support the IEditorCreatable.MandatoryProperties
        /// </summary>
        private int Cylinders
        {
            get { return this.sphereCylinders; }
            set { this.sphereCylinders = value; }
        }
        
        /// <summary>
        /// This property is to support the IEditorCreatable.MandatoryProperties
        /// </summary>
        private int Slices
        {
            get { return this.sphereSlices; }
            set { this.sphereSlices = value; }
        }
        
        #endregion
        
    }
}
